$(function(){
	initOrderList('',$('#accountId').val(),'');
});
function seek(){
	var status=$('#progress').val();
	initOrderList($('#fixOrderId').val(),$('#accountId').val(),status);
}
function initOrderList(fixOrderId,accountId,status){
	if(status==-1)
		status=null;
	$.ajax({
		url:'fixOrder/getByWorker/'+accountId,
		method:'get',
		dataType:'json',
		data:{
			fixOrderId:fixOrderId,
			handleStatus:status,
		},
	    success:function(data){
	    	if(data.code==0){
	    		initOrderTab(data.row)
	    	}
	    	else{
	    	   alert("获取报修记录信息失败")
	    	}
	    },
	    error:function(){
	    	alert("获取报修记录信息出错")
	    }
	});
}

//分页设置
var pageSize=5;
var currentIndex;
var totalCount=0;
var totalPage;

function initOrderTab(list){
	$('#orderTab tbody').empty();
	totalCount=list.length;
	currentIndex=1;
	totalPage=Math.ceil(totalCount/pageSize);
	
	$("#totalPages").html(totalPage);
	$("#currentIndex").html(currentIndex);
	
	var html="";
	for(var i=0;i<list.length;i++){
		var item=list[i];
		var progressCode=item.handleStatus;
		var progress=formatProgress(progressCode);
		html+='<tr id="tr'+i+'" class="tr_item" style="display: none"><td>'+item.id+'</td><td>'+item.article+'</td><td>'+item.description
		      +'</td><td>'+item.createTime+'</td><td>'+progress+'</td>';
		     
	    if(progressCode==4)
	    	html+='<td><a href="javascript:;" onclick="getOrderInfo('+item.id+')">报修详情</a></td></tr>';
	    else
	    	html+='<td><a href="javascript:;" onclick="getOrderInfo('+item.id+')">报修详情</a><br>'
		      +'<a href="javascript:;" onclick="closeOrder('+item.id+','+item.accountId+')">关闭订单</a></td></tr>';
	}
	$('#orderTab tbody').html(html);
	//显示第一页数据
    for(var i=0;i<pageSize;i++){
        var tr=$("#tr"+i);
        if(tr.length>0)
            tr.show();
    }
}

function formatProgress(code){
	var str="";
	switch(code){
	  case 0:str='已取消';break;
	  case 1:str='待处理';break;
	  case 2:str='已处理';break;
	  case 3:str='维修中';break;
	  case 4:str='已完成';break;
	}
	return str;
}
function lastPage() {
   if(currentIndex-1<1)
       alert("当前为第一页")
   else{
       currentIndex--;
       showPage(currentIndex);
   }
}
function nextPage() {
    if(currentIndex+1>totalPage)
        alert("当前为最后一页")
    else{
        currentIndex++;
        showPage(currentIndex);
    }
}
function showPage(index) {
    $("#currentIndex").html(index);
    $(".tr_item").hide();
    for (var i =(index-1)*pageSize; i<totalCount&&i<index*pageSize; i++) {
        $("#tr"+i).show();
    }
}

//关闭订单
function closeOrder(orderId,userId){
	$('#my-confirm').modal({
        relatedTarget: this,
        onConfirm: function(options) {
        	$.post("fixOrder/closeByWorker/"+orderId,
        			{
        		       userId:userId,
        			},
        			function(data){
        		    if(data.code==0){
        		       alert("订单关闭成功，已先客户和管理员报告");
        		       fixOrderListTopic();
        		    }
        		    else
        		    	alert("关闭订单失败")
        	      }
        	);
        },
        // closeOnConfirm: false,
        onCancel: function() {
          //取消操作
        }
      });
}
//获取订单信息
function getOrderInfo(fixOrderId){
	$('.page-content').load("/workerPage/fixOrderInfo/"+fixOrderId);
}

